package com.craftplay.dao;

import com.craftplay.model.ClassOrder;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ClassOrderMapper {
    @Insert({
            "INSERT INTO  classOrder ( `outTradeNo`, `userId`, `unitId`, `money`, `createTime`, `status`, `payTime`, `classType`, `classTreeType`, `classStatus`, `message`)" +
                    " VALUES (#{outTradeNo}, #{userId}, #{unitId}, #{money}, #{createTime}, #{status}, #{payTime}, #{classType}, #{classTreeType}, #{classStatus}, #{message});"
    })
    int insert(ClassOrder record);


    @Select({
            "SELECT count(1) FROM classOrder where unitId =#{unitId} and  userId= #{userId} and status =1 and classStatus!=6 and classStatus!=-2 order by createTime desc  "
    })
    int isPay(@Param("userId") Integer userId, @Param("unitId") Integer unitId);

    @Select({
            "SELECT * FROM classOrder where unitId =#{unitId} and  userId= #{userId}  order by createTime desc  limit 1"
    })
    ClassOrder classStatus(@Param("userId") Integer userId, @Param("unitId") Integer unitId);

    @Select({
            "SELECT * FROM classOrder where id =#{id}"
    })
    ClassOrder selectById(@Param("id") Integer id);

    @Update({
            "update classOrder",
            "set classStatus = classStatus +1 ",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int changeClassStatus(int id);

    @Select({
            "SELECT c.* FROM classOrder c  LEFT JOIN unit u on  c.unitId=u.id  WHERE  u.user_id= #{userId} order by payTime desc  "
    })
    List<ClassOrder> selectByUserId(@Param("userId") int userId);

    @Select({
            "SELECT c. * FROM classOrder c  LEFT JOIN unit u on  c.unitId=u.id order by payTime desc "
    })
    List<ClassOrder> selectAllClass();


    @Select({
            "SELECT * FROM classOrder c  WHERE c.userId= #{userId} and classStatus !=0  order by  payTime desc "
    })
    List<ClassOrder> selectByJoinUserId(@Param("userId") int userId);

    @Select({
            "SELECT * FROM classOrder c  WHERE c.userId= #{userId} and classStatus=0 order by  payTime desc "
    })
    List<ClassOrder> selectByApplyUserId(@Param("userId") int userId);


    @Update({
            "update classOrder",
            "set classStatus = #{classStatus} ",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updateClassStatus(ClassOrder classOrder);


    @Update({
            "update classOrder",
            "set status = #{classStatus} ",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updateStatus(ClassOrder classOrder);
}